home *** CD-ROM | disk | FTP | other *** search
- Path: eua.ericsson.se!usenet
- From: euahjn@eua.ericsson.se (Henrik Johansson)
- Newsgroups: comp.lang.c++
- Subject: Re: Creating a pointer to a function "void (*ptrFunction)()" inside a class
- Date: 5 Jan 1996 11:01:52 GMT
- Organization: Ericsson Telecom Systems Labs, Stockholm, Sweden
- Message-ID: <4cj0f0$ifu@euas20.eua.ericsson.se>
- References: <30ECA10F.3D99@ifu.net>
- Reply-To: euahjn@eua.ericsson.se
- NNTP-Posting-Host: euas31i2c37.eua.ericsson.se
-
- In article <30ECA10F.3D99@ifu.net>, Jason Gresh <gresh@ifu.net> writes:
- > Hi,
- >
- > I am trying to create a base class that has a function that the
- > derived class needs to create. In order to do this, I want to create a
- > pointer to a function in the base class that the derived class can then
- > define. This is not a case where an override will work because the
- > number and names of the derived functions is not known. Hopefully this
- > code sample will make this clearer:
- >
- [snip]
- >
- > If I don't make myFunction part of the derived class (global),
- > everything works fine. As soon as I include it in the class, I cannot
- > assign a pointer to it. I am aware that pointers to functions inside
- > the class include the class name in some way ( this is not an issue for
- > global functions). What is the casting (or other) mechanism to make
- > this work?
- >
- > Thanks,
- >
- > Mike Gresh
-
- How about something like this:
-
- class DerivedClass;
- class BaseClass
- {
- public:
- // int (*ptrFunction)();
- int(DerivedClass::*ptrFunction)(int, int);
- };
-
- class DerivedClass : BaseClass
- {
- DerivedClass::DerivedClass();
- int myFunction(int, int);
- };
-
- DerivedClass::DerivedClass()
- {
- ptrFunction = myFunction;
- }
-
- DerivedClass::myFunction(int, int)
- {
- // code ....
- return 0;
- }
-
-